package com.m3958.firstgwt.client.window;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.firstgwt.client.datasource.DataTemplateDataSource;
import com.m3958.firstgwt.client.layout.ArticleEditLayout;
import com.m3958.firstgwt.client.types.SmartSubOperationName;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartParameterName;
import com.m3958.gwtutil.clientside.client.LoginOutService;
import com.m3958.gwtutil.clientside.client.jso.DataTemplateJso;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

@Singleton
public class DataTemplateSelectWindow extends Window{


	private ListGrid dataTemplateGrid;
	
	private String lastSiteId;
	
	private boolean initialized = false;
	
	private ArticleEditLayout ae;
	
	@Inject
	private AppStatus aservice;
	
	@Inject
	private LoginOutService lios;
	
	@Inject
	private DataTemplateDataSource wsds;
	
	public DataTemplateSelectWindow(){
		setWidth(600);
		setHeight(450);
		setIsModal(true);
		setShowModalMask(true);
		setTitle("文章模板选择");
		centerInPage();
	}
	
	public DataTemplateSelectWindow showMe(ArticleEditLayout ae){
		this.ae = ae;
		if(!initialized){
			initialized = true;
			VLayout layout = new VLayout(5);
			layout.addMember(initGrid());
			layout.addMember(initBt());
			addItem(layout);
		}
		show();
		if(!aservice.getSiteId().equals(lastSiteId)){
			fetchGridData();
			lastSiteId = aservice.getSiteId();
		}
		return this;
	}
	

	private void fetchGridData() {
	    Criteria c = new Criteria();
	    if(!lios.getLoginedUser().isSuperman()){
	    	c.addCriteria(SmartParameterName.SUB_OPERATION_TYPE, SmartSubOperationName.HAS_PERMISSION.toString());
	    }
	    c.addCriteria("_jsonv", "vv");
		c.addCriteria(CommonField.TIMEZONE.getValue(),aservice.getTimeZone());
		dataTemplateGrid.filterData(c);
	}

	private HLayout initBt() {
		HLayout hl = new HLayout();
		hl.setHeight(25);
		hl.setAlign(Alignment.RIGHT);
		IButton ib = new IButton("关闭");
		ib.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				DataTemplateSelectWindow.this.hide();
			}
		});
		hl.addMember(ib);
		return hl;
	}

    
	private ListGrid initGrid() {
		dataTemplateGrid = new ListGrid();
		dataTemplateGrid.setDataSource(wsds);
		dataTemplateGrid.setAutoFetchData(false);
		dataTemplateGrid.setSelectionType(SelectionStyle.SINGLE);
		ListGridField idField = new ListGridField("id");
		ListGridField nameField = new ListGridField(CommonField.NAME.getValue(),"名称");
		dataTemplateGrid.setShowEmptyMessage(true);
		dataTemplateGrid.addRecordClickHandler(new RecordClickHandler() {
			@Override
			public void onRecordClick(RecordClickEvent event) {
				Record r = event.getRecord();
				if(r!=null){
					ae.onArticleTplChange((DataTemplateJso) r.getJsObj());
					DataTemplateSelectWindow.this.hide();
				}
			}
		});
		dataTemplateGrid.setFields(nameField,idField);
		return dataTemplateGrid;
	}
}
